<template>
    <div class="login-container">
        <el-row>
            <el-col :span="12" :xs="0"></el-col>
            <el-col :span="12" :xs="24">
                <el-form ref="loginForms" :model="loginForm" :rules="loginRules" class="login-form">
                    <h1>Hello</h1>
                    <h2>欢迎登录本系统</h2>
                    <el-form-item prop="username">
                        <el-input :prefix-icon="User" v-model="loginForm.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input show-password :prefix-icon="Lock" v-model="loginForm.password"
                            type="password"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button :loading="loading" class="login-btn" type="primary" size="default" @click="login">
                            登录
                        </el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import useUserStore from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { curHourToRangeName } from "@/utils/HourUtil";

let $router = useRouter()
let $route = useRoute()
let userStore = useUserStore()
let loginForm = reactive({ username: 'admin', password: '111111' })
let loading = ref(false)
let loginForms = ref()

const login = async () => {
    //保证表单中各输入部件都校验通过再发请求
    await loginForms.value.validate()
    // 当表单中有输入部件校验不通过时，浏览器控制台有Promise报错日志，这是正常的。

    loading.value = true
    try {
        //当前小时数值 对应的 范围名字
        let curHourRangeName: string = curHourToRangeName()
        await userStore.userLogin(loginForm)
        let redirect:any = $route.query.redirect
        $router.push({ path: redirect || '/' })
        ElNotification({
            type: 'success',
            message: "欢迎回来",
            title: `HI，${curHourRangeName}好`
        })
        loading.value = false
    } catch (error) {
        loading.value = false
        ElNotification({
            type: 'error',
            message: (error as Error).message
        })
    }
}

const validateUserName = (rule: any, value: any, callback: any) => {
    if (value.length >= 5 && value.length <= 10) {
        callback();//校验通过
    } else {
        callback(new Error("用户名为5到10个字符"));//校验失败
    }
}

const validatePassword = (rule: any, value: any, callback: any) => {
    if (value.length >= 5 && value.length <= 15) {
        callback();//校验通过
    } else {
        callback(new Error("密码为5到15个字符"));//校验失败
    }
}


const loginRules = {
    username: [
        // { required: true, message: '用户名不能为空', trigger: 'blur' },
        // { min: 3, max: 10, message: '长度在3到10位之间', trigger: 'change' },
        { trigger: 'change', validator: validateUserName }
    ],
    password: [
        // { required: true, message: '密码不能为空', trigger: 'blur' },
        // { min: 3, max: 15, message: '长度在3到15位之间', trigger: 'change' },
        { trigger: 'change', validator: validatePassword }
    ]
}
</script>

<style scoped lang="scss">
.login-container {
    width: 100%;
    height: 100vh;
    background: url('@/assets/images/background.jpg') no-repeat;
    background-size: cover;

    .login-form {
        width: 80%;
        top: 30vh;
        position: relative;
        background: url('@/assets/images/login_form.png') no-repeat;
        background-size: cover;
        padding: 40px;

        h1 {
            color: white;
            font-size: 40px;
        }

        h2 {
            color: white;
            font-size: 20px;
            margin: 20px 0;
        }

        .login-btn {
            width: 100%;
        }
    }
}
</style>
